package com.skyolder.milkbee.service;

import com.skyolder.milkbee.domain.ExchangeRecord;
import com.skyolder.milkbee.repository.ExchangeRecordRepository;
import com.skyolder.milkbee.service.dto.ExchangeRecordDTO;
import com.skyolder.milkbee.service.mapper.ExchangeRecordMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing ExchangeRecord.
 */
@Service
@Transactional
public class ExchangeRecordService {

	private final Logger log = LoggerFactory.getLogger(ExchangeRecordService.class);

	@Inject
	private ExchangeRecordRepository exchangeRecordRepository;

	@Inject
	private ExchangeRecordMapper exchangeRecordMapper;

	/**
	 * Save a exchangeRecord.
	 *
	 * @param exchangeRecordDTO
	 *            the entity to save
	 * @return the persisted entity
	 */
	public ExchangeRecordDTO save(ExchangeRecordDTO exchangeRecordDTO) {
		log.debug("Request to save ExchangeRecord : {}", exchangeRecordDTO);
		ExchangeRecord exchangeRecord = exchangeRecordMapper.exchangeRecordDTOToExchangeRecord(exchangeRecordDTO);
		exchangeRecord = exchangeRecordRepository.save(exchangeRecord);
		ExchangeRecordDTO result = exchangeRecordMapper.exchangeRecordToExchangeRecordDTO(exchangeRecord);
		return result;
	}

	/**
	 * Get all the exchangeRecords.
	 * 
	 * @param pageable
	 *            the pagination information
	 * @return the list of entities
	 */
	@Transactional(readOnly = true)
	public Page<ExchangeRecordDTO> findAll(Pageable pageable) {
		log.debug("Request to get all ExchangeRecords");
		Page<ExchangeRecord> result = exchangeRecordRepository.findAll(pageable);
		return result.map(exchangeRecord -> exchangeRecordMapper.exchangeRecordToExchangeRecordDTO(exchangeRecord));
	}

	/**
	 * Get one exchangeRecord by id.
	 *
	 * @param id
	 *            the id of the entity
	 * @return the entity
	 */
	@Transactional(readOnly = true)
	public ExchangeRecordDTO findOne(Long id) {
		log.debug("Request to get ExchangeRecord : {}", id);
		ExchangeRecord exchangeRecord = exchangeRecordRepository.findOne(id);
		ExchangeRecordDTO exchangeRecordDTO = exchangeRecordMapper.exchangeRecordToExchangeRecordDTO(exchangeRecord);
		return exchangeRecordDTO;
	}

	/**
	 * Delete the exchangeRecord by id.
	 *
	 * @param id
	 *            the id of the entity
	 */
	public void delete(Long id) {
		log.debug("Request to delete ExchangeRecord : {}", id);
		exchangeRecordRepository.delete(id);
	}

	public Page<ExchangeRecordDTO> findByUserIdAndSchedule(Long userId, String schedule, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<ExchangeRecord> result = exchangeRecordRepository.findByUserIdAndSchedule(userId, schedule, pageable);
		return result.map(exchangeRecord -> exchangeRecordMapper.exchangeRecordToExchangeRecordDTO(exchangeRecord));

	}

	public Page<ExchangeRecordDTO> findBySchedule(String schedule, Pageable pageable) {
		Page<ExchangeRecord> result = exchangeRecordRepository.findBySchedule(schedule, pageable);
		return result.map(exchangeRecord -> exchangeRecordMapper.exchangeRecordToExchangeRecordDTO(exchangeRecord));
	}
}
